package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdSensitiveMapper;
import com.heima.admin.service.AdSensitiveService;
import com.heima.common.exception.CustException;
import com.heima.model.admin.dtos.SensitiveDTO;
import com.heima.model.admin.pojos.AdSensitive;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class AdSensitiveServiceImpl extends ServiceImpl<AdSensitiveMapper, AdSensitive> implements AdSensitiveService {

    @Autowired
    private AdSensitiveMapper adSensitiveMapper;
    @Override
    public ResponseResult list(SensitiveDTO dto) {
        //校验数据
        //检查分页参数
        dto.checkParam();
        //分页对象
        Page<AdSensitive> sensitivePage = new Page<>();
        sensitivePage.setPages(dto.getPage());
        sensitivePage.setSize(dto.getSize());
        //查询条件
        LambdaQueryWrapper<AdSensitive> queryWrapper = Wrappers.lambdaQuery();
        if(StringUtils.isNotBlank(dto.getName())){
            queryWrapper.like(AdSensitive::getSensitives,dto.getName());
        }
        //分页查询
        IPage<AdSensitive> sensitiveIPage = this.page(sensitivePage, queryWrapper);

        ResponseResult responseResult =new PageResponseResult(dto.getPage(),dto.getSize(),sensitiveIPage.getTotal(),sensitiveIPage.getRecords());

        return responseResult;
    }

    @Override
    public ResponseResult insert(AdSensitive adSensitive) {
        //校验数据
        if (StringUtils.isBlank(adSensitive.getSensitives())) {
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID, "敏感词不能为空");
        }
        //判断敏感词是否重复
        LambdaQueryWrapper<AdSensitive> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(AdSensitive::getSensitives,adSensitive.getSensitives());
        int count = this.count(wrapper);
        if (count>0){
            CustException.cust(AppHttpCodeEnum.DATA_EXIST, "敏感词已存在");
        }
        //保存数据库
        adSensitive.setCreatedTime(new Date());
        this.save(adSensitive);

        //响应
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult update(AdSensitive adSensitive) {
        //校验数据
        if (StringUtils.isBlank(adSensitive.getSensitives())) {
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID, "敏感词不能为空");
        }
        //判断敏感词是否重复
        LambdaQueryWrapper<AdSensitive> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(AdSensitive::getSensitives,adSensitive.getSensitives());
        int count = this.count(wrapper);
        if (count>0){
            CustException.cust(AppHttpCodeEnum.DATA_EXIST, "敏感词已存在");
        }
        //修改数据库
        adSensitive.setCreatedTime(new Date());
        this.updateById(adSensitive);

        //响应
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult delete(Integer id) {
        //参数校验
        if (id==null){
            CustException.cust(AppHttpCodeEnum.DATA_EXIST, "敏感词id为空");
        }
        AdSensitive sensitive = this.getById(id);
        if (sensitive==null) {
            CustException.cust(AppHttpCodeEnum.DATA_EXIST, "敏感词不存在");
        }
        //删除数据库
        this.removeById(id);
        //响应
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult<List<String>> selectAllSensitives() {
        return ResponseResult.okResult(adSensitiveMapper.findAllSensitives());
    }
}
